package com.aoxon.cargo.component;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.aoxon.cargo.R;
import com.aoxon.cargo.adapter.BannerAdapter;
import com.aoxon.cargo.util.EnvironmentUtil;

public class Banner implements OnPageChangeListener {

	private MyViewPager mViewPager;
	private LinearLayout mlayout;
	private Drawable ring, circle;
	private View banner;
	private int curPosition = 0;
	protected static final int SCROLL = 1, AUTO_SCROLL_TIME = 5000,
			SETINDEX = 0;
	protected static boolean autoScroll = true;

	public Banner(Activity mActivity) {
		super();
		ring = mActivity.getResources().getDrawable(R.drawable.ring);
		circle = mActivity.getResources().getDrawable(R.drawable.circle);
		banner = LayoutInflater.from(mActivity).inflate(R.layout.banner, null);
		mViewPager = (MyViewPager) banner.findViewById(R.id.banner_pager);
		mViewPager.setOnPageChangeListener(this);
		mlayout = (LinearLayout) banner.findViewById(R.id.banner_ring_line);
		mViewPager.setAdapter(new BannerAdapter(mActivity, mViewPager));
		Log.e("Integer", String.valueOf(Integer.MAX_VALUE / 2));
		Log.v("ViewPager initiled",
				String.valueOf((Integer.MAX_VALUE / 2 - (Integer.MAX_VALUE / 2) % 3) % 3));
		mHandler.sendEmptyMessageDelayed(SCROLL, AUTO_SCROLL_TIME);
		mHandler.sendEmptyMessageDelayed(SETINDEX, 100);
	}

	protected Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			if (SCROLL == msg.what) {
				if (autoScroll) {
					mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1,
							true);
				}
				mHandler.sendEmptyMessageDelayed(SCROLL, AUTO_SCROLL_TIME);
			} else if (SETINDEX == msg.what) {
				mViewPager.setCurrentItem(Integer.MAX_VALUE / 2
						- (Integer.MAX_VALUE / 2) % 3);
			}

			// super.handleMessage(msg);
		}

	};

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int arg0) {
		// TODO Auto-generated method stub
		((ImageView) (mlayout.getChildAt(curPosition))).setImageDrawable(ring);
		curPosition = arg0 % 3;
		((ImageView) (mlayout.getChildAt(curPosition)))
				.setImageDrawable(circle);
	}

	public View getBanner() {
		return banner;
	}

}
@SuppressLint("Instantiatable")
class MyViewPager extends ViewPager {

	static boolean relese = true;

	public MyViewPager(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public MyViewPager(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heigthMeasureSpec) {
		// TODO Auto-generated method stub

		final int width = MeasureSpec.getSize(widthMeasureSpec);
		final int heigth = width / 2;
		for (int i = 0; i < getChildCount(); i++) {
			final View child = getChildAt(i);
			child.measure(width, heigth);
		}
		setMeasuredDimension(width, heigth);
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		final int action = event.getAction();
		final float x = event.getX();
		final int screenWidth = EnvironmentUtil
				.getScreenWidth((Activity) getContext());
		final int edge = screenWidth / 20;
		if (x > edge && x < screenWidth - edge) {
			relese = false;
			getParent().requestDisallowInterceptTouchEvent(true);
			return false;
		}
		relese = true;
		return super.onInterceptTouchEvent(event);
	}

	@Override
	public boolean onTouchEvent(MotionEvent arg0) {
		// TODO Auto-generated method stub
		if (relese)
			return false;
		switch (arg0.getAction()) {
			case MotionEvent.ACTION_DOWN :
				Banner.autoScroll = false;
				break;
			case MotionEvent.ACTION_UP :
				Banner.autoScroll = true;
				break;
			default :
				break;
		}
		return super.onTouchEvent(arg0);
	}

}
